<template>
  <div class="app-container">
    <div v-if="showSearch" class="search-container">
      <el-form :model="initParam" :inline="true">
        <el-form-item label="工序流转单编号">
          <el-input
              v-model="initParam.produceTransferFormCode"
              placeholder="请输入工序流转单编号"
              clearable
              @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="工作中心编号">
          <el-input
              v-model="initParam.workCenterCode"
              placeholder="请输入工作中心编号"
              clearable
              @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="总成物料编号">
          <el-input
              v-model="initParam.materialCode"
              placeholder="请输入物料编号"
              clearable
              @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item label="开工时间">
          <el-date-picker
              v-model="initParam.completeTimeSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss" @change="getstartTime">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchTable"><i-ep-search />搜索</el-button>
          <el-button @click="resetParam"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button
          :disabled="tableData.length === 0"
          @click="
            useDownload(
              'mes/mesproducereport/reportWorkDeductExport',
              '报工扣料统计',
              initParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>
      <!-- 表格主体 -->
      <div v-show="showTable">
        <el-table
          ref="dataTableRef"
          v-loading="loading"
          :data="tableData"
          highlight-current-row
          default-expand-all
          border
          @selection-change="handleSelectionChange"
          row-key="id"
          :tree-props="{
            children: 'details',
            hasChildren: 'hasChildren',
          }"
        >
          <el-table-column
            prop="plantCode"
            label="工厂"
            align="left"
            width="100"
            type=""
          />
          <el-table-column
            prop="workCenterCode"
            label="工作中心"
            minWidth="260"
            align="left"
            type=""
          >
            <template #default="{ row }">
              <span>{{ row.workCenterCode }}-{{ row.workCenterName }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="materialCode"
            label="物料编码"
            minWidth="300"
            align="left"
          >
            <template #default="{ row }">
              <span>{{ row.materialCode }}-{{ row.materialName }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="produceTransferFormCode"
            label="工序流转单"
            minWidth="180"
            align="left"
          ></el-table-column>
          <el-table-column
            prop="produceBatch"
            label="批次号"
            minWidth="180"
            align="left"
          ></el-table-column>

<!--          <el-table-column-->
<!--            prop="shiftName"-->
<!--            label="班次"-->
<!--            minWidth="180"-->
<!--            align="left"-->
<!--          ></el-table-column>-->
<!--          <el-table-column-->
<!--            prop="producePlanCode"-->
<!--            label="生产计划"-->
<!--            minWidth="180"-->
<!--            align="left"-->
<!--          ></el-table-column>-->
          <el-table-column
            prop="productQuantity"
            label="报工数量"
            minWidth="100"
            header-align="center"
            align="right"
          ></el-table-column>
          <el-table-column prop="quantityUnit" label="数量单位" align="left">
            <template #default="{ row }">
              <span>{{
                useDictionary("MeasurementUnit", row.quantityUnit)
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="completeTime"
            label="报工日期"
            minWidth="150"
            align="center"
          >
            <template #default="{ row }">
              <span>{{ row.completeTime?.split(" ")[0] }}</span>
            </template></el-table-column
          >
<!--          <el-table-column
            prop="planDate"
            label="计划日期"
            minWidth="150"
            align="center"
          >
            <template #default="{ row }">
              <span>{{ row.planDate?.split(" ")[0] }}</span>
            </template></el-table-column
          >-->
        </el-table>

        <!-- <el-pagination
          :background="true"
          :current-page="pageable.pageNum"
          :page-size="pageable.pageSize"
          :page-sizes="[10, 25, 50, 100]"
          :total="pageable.total"
          layout="total, sizes, prev, pager, next, jumper"
          class="mt-[20px]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />  -->
      </div>
    </el-card>

    <!-- 列设置 抽屉组件 -->
    <!-- <ColSetting
      v-model:col-setting="tableColumns"
      v-model:search-setting="searchColumns"
      @search-handle="(e:boolean) => (showSearch = e)"
      @col-handle="(e:boolean) => (showTable = e)"
    /> -->
  </div>
</template>

<script setup lang="tsx">
import { useTable, useDictionary, useDownload } from "@/utils/useTable";
import { filterOrder } from "@/utils/commonHooks";
import { getPlantList } from "@/utils/commonData";
defineOptions({
  name: "MESREPORTWORKDEDUCT",
  inheritAttrs: false,
});

/** 自定义设置方法   start **/

// 搜索参数 设置为响应式
const searchColumns: any = reactive([
  {
    type: "input", // 普通输入框
    isShow: true,
    inputType: "text", // 类型, text/textarea
    label: "包装条码", // 标签文本
    fieldName: "externalCode", // 字段名称
    placeholder: "请输入包装条码", // 提示文字
    initValue: "", // 默认值
  },
]);

const resetParam = () => {
  initParam.produceTransferFormCode= "";
  initParam.workCenterCode= "";
  initParam.materialCode= "";
  initParam.completeTimeSearch= "";
  initParam.completeTimeStart= "";
  initParam.completeTimeEnd= "";
  tableData.value = [];
};
// 接收 columns 并设置为响应式
const tableColumns = reactive([
  {
    prop: "plantCode",
    label: "工厂",
    isShow: true,
    minWidth: 180,
    render: (scope: any) => <span>{scope.row?.plantCode ?? ""}</span>,
  },
  {
    prop: "workCenterCode",
    label: "工作中心",
    isShow: true,
    minWidth: 180,
    render: (scope: any) => (
      <span>{`${scope.row.workCenterCode ?? ""}-${
        scope.row.workCenterName ?? ""
      }`}</span>
    ),
  },
  {
    prop: "materialCode",
    label: "物料",
    isShow: true,
    minWidth: 180,
    render: (scope: any) => (
      <span>{`${scope.row.materialCode ?? ""}-${
        scope.row.materialName ?? ""
      }`}</span>
    ),
  },
  {
    prop: "produceTransferFormCode",
    label: "工序流转单号",
    isShow: true,
    minWidth: 180,
  },
  { prop: "produceBatch", label: "批次号", isShow: true, minWidth: 180 },
  { prop: "shiftName", label: "班次", isShow: true, minWidth: 180 },
  {
    prop: "productTime",
    label: "报工日期",
    isShow: true,
    minWidth: 150,
    render: (scope: any) => <span>{scope.row.productTime?.split(" ")}</span>,
  },
  {
    prop: "planDate",
    label: "计划日期",
    minWidth: 150,
    isShow: true,
    render: (scope: any) => <span>{scope.row.planDate?.split(" ")}</span>,
  },
  {
    prop: "materialQuantity",
    label: "报工数量",
    isShow: true,
    minWidth: 100,
    align: "right",
  },
  {
    prop: "quantityUnit",
    label: "数量单位",
    isShow: true,
    minWidth: 100,
  },
  { prop: "completeTime", label: "完工时间", isShow: true, minWidth: 150 },
]);

// 是否显示搜索框
const showSearch = ref(true);

// 是否table
const showTable = ref(true);

/** 列表项自定义方法   end **/

// 请求参数
const requestApi = "mes/mesproducereport/reportWorkDeduct";

// 是否需要分页
const pagination = true;

const initParam = reactive({
  produceTransferFormCode: "",
  workCenterCode: "",
  materialCode: "",
  completeTimeSearch: "",
  completeTimeStart: "",
  completeTimeEnd: "",
});

// 初始化搜索参数数据
const searchParamInit = {};

// 数据请求错误后处理
function requestError() {}

// 数据请求错误后处理
// function dataCallBack() {}   //需要处理
const dataCallBack = ref(undefined); //无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError
);

// 如果初始化表格需要排序=》const initParam = reactive({sort:''});
// 监听页面 initParam 改化，重新获取表格数据
// watch(() => initParam, getTableList, { deep: true });
onMounted(async () => {
  getSelectPlanList();
});
function getstartTime(data: any) {
  if (data){
    initParam.completeTimeStart = data[0]
    initParam.completeTimeEnd = data[1]
  }else {
    initParam.completeTimeStart = ""
    initParam.completeTimeEnd = ""
  }
}
// 监听表格排序方法
function onTableSortChanged(column: any) {
  const { prop, order } = column;

  initParam.sort = {
    column: prop,
    orderBy: filterOrder(order),
  };
}

// 获取头部导航
async function getSelectPlanList() {
  const list = await getPlantList();
  list.map((item) => {
    item.label = `${item.organizationCode}`;
    item.value = `${item.id}`;
  });
  searchColumns[0].optionList = list;
}

const searchTable = () => {
  console.log(initParam);
  if (!initParam.produceTransferFormCode && !initParam.workCenterCode && !initParam.materialCode && !initParam.completeTimeStart && !initParam.completeTimeEnd) {
    ElMessage.error("请输入查询条件");
    return;
  }
  getTableList();
};
</script>
